package com.hugesoft.jxgs.activity;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.utils.BitmapUtil;
import com.hugesoft.utils.FileUtil;
import com.hugesoft.utils.Util;

public class CaptureActivity extends BaseActivity implements OnClickListener {

	ImageView[] iViews = new ImageView[4];
	String[] imgPath = new String[4];
	public static String TAGIMGPATH = "TAGIMGPATH";
	private static final String TAG = "CaptureActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initUI();
	}

	private void initUI() {
		setContentView(R.layout.activitycapture);
		((TextView) findViewById(R.id.titleTextView)).setText("照片");
		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setFinishDataAndFinish();
			}
		});

		iViews[0] = (ImageView) findViewById(R.id.imageView1);
		iViews[1] = (ImageView) findViewById(R.id.imageView2);
		iViews[2] = (ImageView) findViewById(R.id.imageView3);
		iViews[3] = (ImageView) findViewById(R.id.imageView4);

		for (int i = 0; i < iViews.length; i++) {
			iViews[i].setOnClickListener(this);
		}

		Intent i = getIntent();
		String picPath = i.getStringExtra(TAGIMGPATH);
		if (picPath!= null) 
		{
			imgPath = picPath.split(Constans.PICPATHSPLITOR);
			
			for (int j = 0; j < imgPath.length; j++) {
				Bitmap b = (Bitmap) BitmapUtil.loadBitmap(imgPath[j]);
				if (b==null||b.isRecycled()) {
					continue;
				}
				iViews[j].setImageBitmap(b);
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.imageView1:
		case R.id.imageView2:
		case R.id.imageView3:
		case R.id.imageView4:
			Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			File dir = new File(Environment.getExternalStorageDirectory()
					+ "/hugesoft/jxgs/capture");
			if (!dir.exists()) {
				dir.mkdirs();
			}
			camera.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(dir, "tmp.jpg")));
			try {
				startActivityForResult(camera,arg0.getId());
			} catch (Exception e) {
//				Log.i(TAG, );
				Util.showToast(CaptureActivity.this, "没有摄像头吧");
			}
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode==Activity.RESULT_OK) {
			String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";	
			Bitmap b = (Bitmap) BitmapUtil.loadBitmap(Environment.getExternalStorageDirectory()+"/hugesoft/jxgs/capture/tmp.jpg");
			if (b==null||b.isRecycled()) {
				Util.showToast(this, "获取照片失败,请重试");
				return;
			}
			Bitmap thumbnail = BitmapUtil.comp(b, 400f, 400f);
			String path = FileUtil.saveBitmapToSdCard(b, Environment.getExternalStorageDirectory().getAbsolutePath()+"/hugesoft/jxgs/capture/");
			b.recycle();
			b = null;
			System.gc();
//			String thumb_path = FileUtil.saveBitmapToSdCard(thumbnail, Environment.getExternalStorageDirectory().getAbsolutePath());
//			Log.i("thumb_path", thumb_path);
			switch (requestCode) {
			case R.id.imageView1:
				iViews[0].setImageBitmap(thumbnail);
				imgPath[0] = path;
				break;
			case R.id.imageView2:
				iViews[1].setImageBitmap(thumbnail);
				imgPath[1] = path;
				break;
			case R.id.imageView3:
				iViews[2].setImageBitmap(thumbnail);
				imgPath[2] = path;
				break;
			case R.id.imageView4:
				iViews[3].setImageBitmap(thumbnail);
				imgPath[3] = path;
				break;

			default:
				break;
			}
//			thumbnail.recycle();
//			thumbnail = null;
//			System.gc();
			
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			setFinishDataAndFinish();
        }
        return super.onKeyUp(keyCode, event);
	}
	
	private void setFinishDataAndFinish()
	{
		String picpath = "";
		for (int i = 0; i < imgPath.length; i++) {
			picpath += imgPath[i];
			picpath += Constans.PICPATHSPLITOR;
		}
		Intent i = new Intent();
		i.putExtra(TAGIMGPATH,picpath);
		setResult(0,i);
		finish();
	}
}
